class CoordsCalc
{
  CoordsCalc()
  {
  }
  
  void update()
  {
    int currX = vidW;
    int currW = 0;
    boolean isYAssigned = false;
    boolean isWAssigned = false;
    for (int j = 0; j < coloursAssigned; j++) 
    {
      currX = vidW;
      currW = 0;
      isYAssigned = false;
      isWAssigned = false;
      for (int i = 0; i < numPixels; i++)
      {
        colouredPixels[abs(i / vidW)][i % vidW] = 0;
        color currColor = cam.pixels[i];
        currR = (currColor >> 16) & 0xFF;
        currG = (currColor >> 8) & 0xFF;
        currB = currColor & 0xFF;
        if(isColourWithinRange(j))
        {
          noStroke();
          if (isShowPixels) 
          {
            fill(pixelColours[j]);
            rect((i % vidW), (abs(i / vidW)), 1, 1);
            rect((i % vidW) * rectDivide, (abs(i / vidW)) * rectDivide, 1 * rectDivide, 1 * rectDivide);
          }
          if ((i % vidW) < currX)
          {
            currX = i % vidW;
            squareCoords[j][0] = currX;
          } 
          if (!isYAssigned)
          {
            isYAssigned = true;
            squareCoords[j][1] = abs(i / vidW);
          }
          squareCoords[j][3] = (abs(i / vidW)) - squareCoords[j][1] + 1;
          if((i % vidW) > currW)
          {
            currW = i % vidW;
            isWAssigned = true;
          }
        }
        if(i == numPixels - 1 && isWAssigned)
        {
          squareCoords[j][2] = currW - squareCoords[j][0] + 1;
        }
      }
    }
    for (int i = 0; i < coloursAssigned; i++)
    {
      centrePoints[i][0] = (squareCoords[i][0] * rectDivide) + ((squareCoords[i][2] * rectDivide) / 2);
      centrePoints[i][1] = (squareCoords[i][1] * rectDivide) + ((squareCoords[i][3] * rectDivide) / 2);
      fill(0, 0, 0);
      ellipse(centrePoints[i][0], centrePoints[i][1], 10, 10);
    }
  }

  boolean isColourWithinRange(int j)
  {
    if(currR > (colourCompareData[j][0] + colourRange) || currR < (colourCompareData[j][0] - colourRange))
    {
      return false;
    }
    if(currG > (colourCompareData[j][1] + colourRange) || currG < (colourCompareData[j][1] - colourRange))
    {
      return false;
    }
    if(currB > (colourCompareData[j][2] + colourRange) || currB < (colourCompareData[j][2] - colourRange))
    {
      return false;
    }
    return true;
  }
}
